package com.zookeeper.mine.study.controller;

import com.zookeeper.mine.study.entity.User;
import com.zookeeper.mine.study.service.UserService;
import com.zookeeper.mine.study.utils.TestUtils;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

//    @Autowired
//    private ZooKeeper zooKeeper;
//
//    @Autowired
//    private TestUtils testUtils;

    @RequestMapping("/")
    public String test() {

        return "aaa";
    }

    @RequestMapping("/find")
    public String findById(Long id) {

        User user = userService.findById(id);

        System.out.println("find---->" + user.toString());

        return "find";
    }

    @RequestMapping("/findAll")
    public String findAll() {

        List<User> users = userService.findAll();

        System.out.println("findAll---->" + users.size());

        return "findAll";
    }

    @RequestMapping("/add")
    public String insert(String name) {

        User user = new User();
        user.name = name;

        user.id = userService.insert(user);

        System.out.println("add---->"+user.toString());

        return "insert";
    }

//    @RequestMapping("/test")
//    public String test1() {
//
//        if(!StringUtils.isEmpty(testUtils.weim)) {
//            System.out.println(testUtils.weim);
//        }
//
//        byte [] data = new byte[100];
//
//        try {
//            data = zooKeeper.getData("/weim",false,null);
//        } catch (KeeperException e) {
//            e.printStackTrace();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//        testUtils.weim = new String(data);
//
//        System.out.println(testUtils.weim);
//
//        return testUtils.weim;
//    }


}
